using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Unibot
{
    /*
     * This class does not contain the actual sprite.  It is used
     * to tell elements which part of a sprite it should use.
     */ 
    public class Animation
    {
        float timer = 0f; // The base time, increases
        float fps = 1000f / 25f; // 25 frames per second

        int numFrames; // Number of frames the animation has
        int currentFrame; // The current frame of the animation

        int spriteWidth; // Width of the animation
        int spriteHeight; // Height of the animation
        int startFrame; // The frame where this animation starts

        Rectangle animationSource; // The area of the sprite of which will be drawn

        // Counting starts at 0
        // TODO : public Animation(ActionState state,int width, int height, int start, int frames)
        public Animation(int width, int height, int start, int frames)
        {
            currentFrame = start;
            startFrame = start;
            this.spriteWidth = width;
            this.spriteHeight = height;
            numFrames = frames;
            this.animationSource = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        }
        public void updateFrames(GameTime gt)
        {
            timer += (float)gt.ElapsedGameTime.TotalMilliseconds;
            // If it is time for the animations next frame
            if (timer > fps)
            {
                currentFrame++;
                // Go back to first part of animation
                if (currentFrame > numFrames - 1)
                {
                    currentFrame = startFrame;
                }
                timer = 0f;
            }
            this.animationSource = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        }

        /* 
         * Returns the next frame to be used in the animation.
         */ 
        public Rectangle getNextFrame()
        {
            return this.animationSource;
        }
    }
}
